package com.cynteka.cyfoman.core.collection;

import com.cynteka.cyfoman.core.IValueObject;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 19:35
 */
public class DBSimpleCollectionApppender implements IRegistryAppender {
    private DataSource dataSource;
    private ISQLGenerator sqlGenerator = new BaseSQLGenerator();
    private String tableName;
    private String sql;
    private String collectionId;
    private String fieldId = "id";

    public class Collection extends BaseCollection<IValueObject<Integer, String>> implements IDBCollection<IValueObject<Integer, String>> {

        public Collection() {
            super(collectionId);
        }

        @Override
        public java.util.Collection<IValueObject<Integer, String>> getItems(Map<String, Object> params) {
            if (sql == null)
                sql = sqlGenerator.generateSimpleSelect(tableName);
            List<IValueObject<Integer, String>> res = new DBUtil<Integer>().queryForValueObjects(dataSource, sql, fieldId);
            return res;
        }

        @Override
        public String getTableName() {
            return tableName;
        }
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public ISQLGenerator getSqlGenerator() {
        return sqlGenerator;
    }

    public void setSqlGenerator(ISQLGenerator sqlGenerator) {
        this.sqlGenerator = sqlGenerator;
    }

    @Override
    public ICollection[] getCollections() {
        return new ICollection[]{new Collection()};  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ILink[] getLinks() {
        return new ILink[0];
    }

    public String getCollectionId() {
        return collectionId;
    }

    public void setCollectionId(String collectionId) {
        this.collectionId = collectionId;
    }

    public String getFieldId() {
        return fieldId;
    }

    public void setFieldId(String fieldId) {
        this.fieldId = fieldId;
    }
}
